<template>
	<view class="mainpadding2">
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">領取管道</view>
			<u-radio-group v-model="type" placement="row" circle>
				<u-radio activeColor="#ff8505" :customStyle="{marginRight: '30rpx'}" label="自動領取" name="1"></u-radio>
				<u-radio activeColor="#ff8505" label="排隊領取" name="2"></u-radio>
			</u-radio-group>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">免運券名</view>
			<input type="text" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入免運券名稱"
				v-model="name">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">最低使用金額</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入最低使用金額"
				v-model="di_money">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius">
			<view class="hei_text bold titletext">優惠券張數</view>
			<input type="number" class="hei_text main_size bold" style="text-align: right;" placeholder="請輸入優惠券張數" v-model="num">
		</view>
		<view class="ffffff mainpadding_top flexbetween radius"  @click="messageTime.timeShow=true;timevalue=0">
			<view class="hei_text bold titletext">開始時間</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="starttime" placeholder="請選擇開始時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<view class="ffffff mainpadding_top flexbetween radius"  @click="messageTime.timeShow=true;timevalue=1">
			<view class="hei_text bold titletext">結束時間</view>
			<view class="flexright">
				<input type="text" class="hei_text main_size bold margin_right1" style="text-align: right;" disabled="" v-model="endtime" placeholder="請選擇結束時間">
				<u-icon name="arrow-right" color="#999" size="16"></u-icon>
			</view>
		</view>
		<timePicker :messageTime="messageTime" @timeConfirm="timeConfirm"></timePicker>
		<view class="submitbtn margin_top" @click="submit">確認</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: "",
				dk_money: "",
				di_money: "",
				num: "",
				fenshop_id: "",
				starttime: "",
				endtime: "",
				messageTime: {
					timeShow: false,
					title: "選擇時間",
					mode: "datetime", //date日期，datetime年月日时分秒，time時間，year-month年月
				},
				timevalue: 0, //0開始，1结束
				listid: "",
				type:"1",
				issubmit:false
			}
		},
		onLoad(options) {
			if (options.id) {
				this.listid = options.id
				uni.setNavigationBarTitle({
					title: "編輯優惠券"
				})
				this.getDetail()
			}
		},
		methods: {
			getDetail() {
				this.http.request("/api/shop/shipdeail", 'POST', {
					id: this.listid
				}).then(res => {
					let data = res.data
					this.name = data.name
					this.di_money = data.di_money
					this.num = data.num
					this.type = data.type.toString()
					this.starttime = this.http.timereturn(data.starttime * 1000)
					this.endtime = this.http.timereturn(data.endtime * 1000)
				})
			},
			timeConfirm(e) {
				if (this.timevalue == 0) {
					this.starttime = e
				} else {
					this.endtime = e
				}
			},
			async submit() {
				if(this.issubmit) return
				if (this.name == "") {
					this.http.toast("請輸入優惠券名稱")
					return false
				}
				if (this.di_money == "") {
					this.http.toast("請輸入最低使用金額")
					return false
				}
				if (this.num == "") {
					this.http.toast("請輸入優惠券數量")
					return false
				}
				if (this.starttime == "") {
					this.http.toast("請選擇開始時間")
					return false
				}
				if (this.endtime == "") {
					this.http.toast("請選擇結束時間")
					return false
				}
				let datamsg = {
					shop_id: uni.getStorageSync("shop_id"),
					name: this.name,
					di_money: this.di_money,
					num: this.num,
					starttime: this.starttime,
					endtime: this.endtime,
					type:this.type
				}
				let url = "/api/shop/addship"
				if (this.listid) {
					url = "/api/shop/editship"
					this.$set(datamsg, "id", this.listid)
				}
				this.issubmit = true
				uni.showLoading({
					mask:true,
					title:"請稍後"
				})
				let res = await this.http.request(url, 'POST', datamsg)
				uni.hideLoading()
				if (res.code == 1) {
					this.http.toast(res.msg)
					setTimeout(() => {
						uni.navigateBack(1)
					}, 500)
				} else {
					this.issubmit = false
					this.http.toast(res.msg)
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
::v-deep.u-radio-group {
		flex: none !important;
	}
</style>